package hanlp;

import com.hankcs.hanlp.suggest.Suggester;
import com.hankcs.hanlp.suggest.scorer.BaseScorer;
import com.hankcs.hanlp.suggest.scorer.editdistance.EditDistanceScorer;
import com.hankcs.hanlp.suggest.scorer.lexeme.IdVectorScorer;
import com.hankcs.hanlp.suggest.scorer.pinyin.PinyinScorer;

import java.util.*;
import java.util.Map.Entry;

/**
 * Created by zhoub on 2020/8/5.
 */
public class MySuggester  extends Suggester{

    List<BaseScorer> scorerList;

    public MySuggester() {
        this.scorerList = new ArrayList();
        this.scorerList.add(new IdVectorScorer());
        this.scorerList.add(new EditDistanceScorer());
        this.scorerList.add(new PinyinScorer());
    }

    public List<String> suggest(String key, int size) {
        List<String> resultList = new ArrayList(size);
        TreeMap<String, Double> scoreMap = new TreeMap();
        Iterator var5 = this.scorerList.iterator();

        while(var5.hasNext()) {
            BaseScorer scorer = (BaseScorer)var5.next();
            Map<String, Double> map = scorer.computeScore(key);
            Double max = max(map);

            Entry<String,Object> entry;
            Double score;
            for(Iterator var9 = map.entrySet().iterator(); var9.hasNext(); scoreMap.put(entry.getKey(), Double.valueOf(score.doubleValue() / max.doubleValue() + ((Double)entry.getValue()).doubleValue() * scorer.boost))) {
                entry = (Map.Entry)var9.next();
                score = (Double)scoreMap.get(entry.getKey());
                if(score == null) {
                    score = Double.valueOf(0.0D);
                }
            }
        }

        var5 = sortScoreMap(scoreMap).entrySet().iterator();

        while(var5.hasNext()) {
            Map.Entry<Double, Set<String>> entry = (Map.Entry)var5.next();
            Iterator var13 = ((Set)entry.getValue()).iterator();

            while(var13.hasNext()) {
                String sentence = (String)var13.next();
                if(resultList.size() >= size) {
                    return resultList;
                }

                resultList.add(sentence);
            }
        }

        return resultList;
    }

    private static TreeMap<Double, Set<String>> sortScoreMap(TreeMap<String, Double> scoreMap) {
        TreeMap<Double, Set<String>> result = new TreeMap(Collections.reverseOrder());

        Entry<String,Double> entry;
        Set<String> sentenceSet;
        for(Iterator var2 = scoreMap.entrySet().iterator(); var2.hasNext(); sentenceSet.add(entry.getKey())) {
            entry = (Entry)var2.next();
            sentenceSet = (Set)result.get(entry.getValue());
            if(sentenceSet == null) {
                sentenceSet = new HashSet();
                result.put(entry.getValue(), sentenceSet);
            }
        }

        return result;
    }

    private static Double max(Map<String, Double> map) {
        Double theMax = Double.valueOf(0.0D);

        Double v;
        for(Iterator var2 = map.values().iterator(); var2.hasNext(); theMax = Double.valueOf(Math.max(theMax.doubleValue(), v.doubleValue()))) {
            v = (Double)var2.next();
        }

        return theMax;
    }

}
